package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetAdsParam(param map[string]interface{}, order string) (*models_main.Ads, error) {
	fName := "GetAdsParam"
	o := &models_main.Ads{}
	table := mysql.GetRTable(&models_main.Ads{})
	mysql.Condition(table, param, "", order, 0, 0)
	err := table.Take(o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetAdsListParam(param map[string]interface{}, order string, page, size int) ([]*models_main.Ads, error) {
	fName := "GetAdsListParam"
	o := make([]*models_main.Ads, 0)
	table := mysql.GetRTable(&models_main.Ads{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetAdsListParam2(param map[string]interface{}, order string) ([]*models_main.Ads, error) {
	fName := "GetAdsListParam2"
	o := make([]*models_main.Ads, 0)
	table := mysql.GetRTable(&models_main.Ads{})
	mysql.Condition(table, param, "", order, 0, 0)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s %s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetAvatarListParam(param map[string]interface{}, order string, page, size int) ([]*models_main.Avatars, error) {
	fName := "GetAvatarListParam"
	o := make([]*models_main.Avatars, 0)
	table := mysql.GetRTable(&models_main.Ads{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
